// pages/index/wineShop/orderPay/orderPay.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    couponVal:0,
    pick1:'../../../../imgs/pick2.png',
    pick2:'../../../../imgs/pick2.png',
    id1:'active1',
    id2:'active2',
    sty:3
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    console.log('optionsoptions',options)
    let id = parseInt(options.id)
    that.setData({
      id:id,
      drinks: options.drinks,
      house_num:options.house_num,
      order_price:options.order_price,
      coupon: options.coupon,
      wineOrder:options.wineOrder      
    })
      // wx.request({
      //   url:app.globalData.baseUrl+'/api/drinks/drinks_info',
      //   data:{
      //     session_key:app.gs(),
      //     id:id
      //   },
      //   success:function(res){
      //     console.log(res);
      //     let drinks_info = res.data.data;
      //     let allPrice = 0;
      //     let acturePrice = 0;
      //     let couponVal;
      //     if (drinks_info.coupons){
      //        couponVal = drinks_info.coupons.amount;
      //     }else {
      //        couponVal = 0;
      //     }
          
      //     for (let i = 0; i < drinks_info.list.length;i++){
      //       allPrice = allPrice + drinks_info.list[i].unit_price * drinks_info.list[i].buynum
      //     }
      //     acturePrice = allPrice - couponVal;
      //     console.log('acturePrice',acturePrice)
      //     console.log(allPrice)
      //     that.setData({
      //       drinks_info:drinks_info,
      //       allPrice: allPrice,
      //       acturePrice: acturePrice,
      //       order_id:id
      //     })
      //   }
      // })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that = this;
    let id = parseInt(that.data.id);
    console.log(',,,', wx.getStorageSync('house_num'))
    if (wx.getStorageSync('house_num')){
        that.setData({
          house_num: wx.getStorageSync('house_num')
        })
    }
    wx.request({
      url: app.globalData.baseUrl + '/api/drinks/drinks_info',
      data: {
        session_key: app.gs(),
        id: id
      },
      success: function (res) {
        console.log(res);
        let drinks_info = res.data.data;
        let allPrice = 0;
        let acturePrice = 0;
        let couponVal;
        if (drinks_info.coupons) {
          couponVal = drinks_info.coupons.amount;
        } else {
          couponVal = 0;
        }
        for (let i = 0; i < drinks_info.list.length; i++) {
          allPrice = allPrice + drinks_info.list[i].unit_price * drinks_info.list[i].buynum
        }
        if(that.data.coupon==0){
          acturePrice = allPrice
        } else if (that.data.coupon == 1){
          acturePrice = allPrice - couponVal
        }
       
        console.log(allPrice)
        console.log('iddddd',id)

        acturePrice = (acturePrice*1).toFixed(2);
        if (acturePrice < 0){
          acturePrice = 0
        }
        console.log(acturePrice)
        that.setData({
          drinks_info: drinks_info,
          allPrice: allPrice,
          acturePrice: acturePrice,
          order_id: id
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  chooseCoupon(e){
    let _this = this;
    if (!_this.data.drinks_info.coupons){
      wx.showToast({
        title: '暂无优惠券',
      })
      return
    }
    wx.setStorageSync('house_num', _this.data.house_num)
    wx.navigateTo({
      // url: 'chooseCoupon/chooseCoupon?sty=3' + '&order_id=' + _this.data.order_id + '&drinks=' + _this.data.drinks + '&house_num=' + _this.data.house_num + '&order_price=' + _this.data.order_price 
      url: 'chooseCoupon/chooseCoupon?sty=3' + '&order_id=' + _this.data.order_id 
    })
  },
  payStyle(e){
    // console.log(e)
    let _this = this;
     if(e.currentTarget.id=='active2'){
        _this.setData({
          pick2:'../../../../imgs/pick1.png',
          pick1:'../../../../imgs/pick2.png',
          sty:e.currentTarget.dataset.sty
        })
     }else {
       _this.setData({
         pick1: '../../../../imgs/pick1.png',
         pick2: '../../../../imgs/pick2.png',
         sty: e.currentTarget.dataset.sty
       })
     }
  },
  payIng(e){
    let _this = this;
    app.wxrequest({
      url: app.globalData.baseUrl +'/api/pay/payByOrder',
      data:{
        session_key:app.gs(),
        type:_this.data.sty,
        order_id: _this.data.order_id,
        form_id:e.detail.formId
      },
      success(res){
        console.log('resres',res)
        wx.hideToast();
      if(_this.data.coupon==1){
        console.log('8888')
        if (res.data.message == '支付成功') {
          console.log('9999')
           wx.setStorageSync('drinksShop',[]);
          wx.setStorageSync('allPrice',0);
          wx.setStorageSync('allCount',0);
          wx.setStorageSync('goods',[]);
            setTimeout(function () {
            wx.navigateBack({
              delta: 2
            })
          }, 2000)
     
        }else {
          if (_this.data.sty == 1){
            wx.requestPayment({
              timeStamp: res.data.data.timeStamp,
              nonceStr: res.data.data.nonceStr,
              package: res.data.data.package,
              signType: res.data.data.signType,
              paySign: res.data.data.paySign,
              success(res) {
                console.log(res)
                if (res.errMsg == 'requestPayment:ok') {
                  wx.showToast({
                    title: '支付成功',
                  })
                  wx.setStorageSync('drinksShop', []);
                  wx.setStorageSync('goods', []);
                  wx.setStorageSync('allPrice', 0);
                  wx.setStorageSync('allCount', 0);
                  // console.log('111111111')
                  if (_this.data.coupon == 1) {
                    console.log('6666666666')
                    setTimeout(function () {
                      wx.navigateBack({
                        delta: 2
                      })
                    }, 2000)
                  } else {
                    // console.log('222222222')
                    setTimeout(function () {
                      wx.navigateBack({
                        delta: 1
                      })
                    }, 2000)
                  }

                }
              }
            })
          }
       
        }
      }else if(_this.data.coupon==0){
          if(_this.data.sty==1){
            console.log('444444444')
            wx.requestPayment({
              timeStamp: res.data.data.timeStamp,
              nonceStr: res.data.data.nonceStr,
              package: res.data.data.package,
              signType: res.data.data.signType,
              paySign: res.data.data.paySign,
              success(res) {
                console.log(res)
                if (res.errMsg == 'requestPayment:ok') {
                  wx.showToast({
                    title: '支付成功',
                  })
                  wx.setStorageSync('drinksShop', []);
                  wx.setStorageSync('goods', []);
                  wx.setStorageSync('allPrice', 0);
                  wx.setStorageSync('allCount', 0);
               
                  if (_this.data.coupon == 1) {
                    setTimeout(function () {
                      wx.navigateBack({
                        delta: 2
                      })
                    }, 2000)
                  } else {
                    setTimeout(function () {
                      wx.navigateBack({
                        delta: 1
                      })
                    }, 2000)
                  }

                }
              }
            })
          }else if (_this.data.sty==2){
            console.log('5555555')
            if (res.data.code == 200) {
              wx.showToast({
                title: '支付成功',
              })
              wx.setStorageSync('drinksShop', []);
              wx.setStorageSync('goods', []);
              wx.setStorageSync('allPrice', 0);
              wx.setStorageSync('allCount', 0);
              if (_this.data.coupon == 0) {
                setTimeout(function () {
                  // console.log('1111111111')
                  wx.navigateBack({
                    delta: 1
                  })
                }, 2000)
              }
            }
            if (res.data.message == 402) {
              wx.showToast({
                title: '余额不足',
              })
            }
          }
      }
        else if (_this.data.sty == 3){
          wx.showToast({
            title: '请选择支付方式',
          })
        }
      }
    })
  }
})